﻿Imports System.Data.SqlClient
Public Class FrmTKHangHoa
    Private Sub FrmTKHangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbxDVT.DataSource = TruyVanDanhSach("select * from DonVi")
        cbxDVT.DisplayMember = "TenDV"
        cbxDVT.ValueMember = "MaDV"
        cbxDVT.SelectedIndex = -1
        UpdateList()
    End Sub

    Public Function TruyVanDanhSach(ByVal strSql As String) As DataTable
        Dim myConn As New SqlConnection(chuoiketnoi)
        Dim myTable As New DataTable
        Try
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(strSql, myConn)
            myAdapter.Fill(myTable)
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
        Return myTable
    End Function

    Private Sub UpdateList()
        Dim strSql As String = "select ROW_NUMBER() OVER (ORDER BY hh.MaHH) as STT, hh.MaHH as [Mã hàng hóa], hh.TenHH as [Tên hàng hóa], dv.TenDV as [Đơn vị tính], hh.DienGiai as [Diễn giải], hh.MaDV from HangHoa hh left join DonVi dv on dv.MaDV = hh.MaDV "
        Dim myConn As New SqlConnection(chuoiketnoi)
        Dim myTable As New DataTable
        Try
            myConn.Open()
            If radMaHH.Checked Then
                If txtMaHH.Text <> "" Then
                    strSql &= " Where hh.MaHH like N'%" & txtMaHH.Text & "%'"
                End If
            ElseIf radTenHH.Checked Then
                If txtTenHH.Text <> "" Then
                    strSql &= " Where hh.TenHH like N'%" & txtTenHH.Text & "%'"
                End If
            Else
                strSql &= " Where dv.MaDV = " & cbxDVT.SelectedValue
            End If
            strSql &= " order by STT"
            Dim myAdapter As New SqlDataAdapter(strSql, myConn)
            myAdapter.Fill(myTable)
            dgvList.DataSource = myTable
            dgvList.Columns.Item(0).Width = 50
            dgvList.Columns.Item(1).Width = 100
            dgvList.Columns.Item(2).Width = 300
            dgvList.Columns.Item(3).Width = 100
            dgvList.Columns.Item(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            dgvList.Columns.Item(5).Visible = False
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
    End Sub

    Private Sub btnTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTim.Click
        UpdateList()
    End Sub

    Private Sub btnDong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub radMaHH_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radMaHH.CheckedChanged
        If radMaHH.Checked Then
            txtMaHH.ReadOnly = False
        Else
            txtMaHH.ReadOnly = True
        End If
    End Sub

    Private Sub radTenHH_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radTenHH.CheckedChanged
        If radTenHH.Checked Then
            txtTenHH.ReadOnly = False
        Else
            txtTenHH.ReadOnly = True
        End If
    End Sub

    Private Sub radDV_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radDV.CheckedChanged
        If radDV.Checked Then
            cbxDVT.Enabled = True
        Else
            cbxDVT.Enabled = False
        End If
    End Sub
End Class